const db = require('../model/db.js');//引入数据库操作的模块
const md5 = require('md5');//引入md5加密的模块
const ObjectId = require('mongodb').ObjectId;

//显示首页
exports.showIndex = (req, res) => {
    if (req.session.user) {//表示已登录状态
        if (req.query.dir == 1) {//dir参数是"1"表示退出登录
            req.session.user = null;
            db.find('myblog', 'message', function (msg) {
                res.render('index1', {//因此呈现未登录页面
                    msg: msg
                });
            });

        } else {
            db.find('myblog', 'message', function (msg) {
                res.render('index2', {
                    msg: msg,
                    uname: req.session.user
                });
            });
        }
    } else {
        db.find('myblog', 'message', function (msg) {
            res.render('index1', {
                msg: msg
            });
        });
    }
};
//登录判断
exports.login = (req, res) => {
    let str = {
        username: req.body.uname,
        pwd: md5(md5(req.body.pwd))
    };
    db.find('myblog', 'users', function (result) {
        if (result.length == 0) {
            res.send(false);
        } else {
            req.session.user = req.body.uname;
            res.send(true);
        }
    }, str);
};
//注册路由
exports.regit = (req, res) => {
    let str = {
        username: req.body.uname
    };
    db.find('myblog', 'users', function (result) {
        if (result.length == 0) {
            str1 = {
                username: req.body.uname,
                pwd: md5(md5(req.body.pwd)),
                pic: "https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture"
            };
            db.insert('myblog', 'users', str1, function (result1) {
                res.send(result1);
            });
        } else {
            // console.log(12);
            res.send('0');
        }
    }, str);
};
//获取用户信息
exports.query = (req, res) => {
    if (req.query.dir == "1") {
        db.find('myblog', 'users', function (result) {
            res.send(result);
        });
    }else if(req.query.dir == "2"){
        db.find('myblog', 'message', function (result) {
            res.send(result);
        });
    }
};
//发布微博
exports.publish = (req, res) => {
    let str = {
        username: req.query.username,
        pic: req.query.pic,
        content: req.query.content,
        date: req.query.date,
        like: req.query.like,
        comment: req.query.comment
    };
    db.insert('myblog', 'message', str, function (result) {
        res.send(result);
    });
};
//评论已有的微博
exports.remark = (req, res) => {
    let str = {
        _id: ObjectId(req.query.id)
    };
    // console.log(str._id);
    // res.send();
    db.find('myblog', 'message', function (result) {
        if (result[0].comment == undefined) {
            let newTarget = {
                comment: [{
                    username: req.query.username,
                    msg: req.query.msg,
                    date: req.query.date
                }]
            };
            db.update('myblog', 'message', str, newTarget, function (result1) {
                res.send(result1);
            });
        } else {
            let arr = [];
            result[0].comment.forEach(function (ele) {
                arr.push(ele);
            });
            arr.push({
                username: req.query.username,
                msg: req.query.msg,
                date: req.query.date
            });
            let newTarget = {
                comment: arr
            };
            db.update('myblog', 'message', str, newTarget, function (result1) {
                res.send(result1);
            });
        }
    }, str);
};
//删除和点赞
exports.others = (req, res) => {
    let str = {
        _id: ObjectId(req.query.id)//根据微博id找到对应评论的文档
    };
    if (req.query.dir == "0") {//发送dir参数为"0"表示删除功能
        db.delete('myblog', 'message', str, function (result) {//执行删除文档操作
            res.send(result);//删除成功返回true信号
        });
    } else if (req.query.dir == "1") {//发送dir参数为"1"表示更新点赞数量
        let newTarget = {
            like: req.query.like
        };
        db.update('myblog', 'message', str, newTarget, function (result) {
            res.send(result);
        });
    }
};
//功能注销用户
exports.remove = (req, res) => {
    let str = {
        username: req.query.uname
    };
    db.delete('myblog', 'users', str, function (result) {//执行删除文档操作
        req.session.user = null;
        res.send(result);//删除成功返回true信号
    });
}